package com.sky.flyweight;

import java.util.concurrent.ConcurrentHashMap;


class JULLogger {

    private String name;

    JULLogger(String name) {

        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    void debug(String message) {
        System.out.println(" DEBUG " + message);
    }

    void info(String message) {
        System.out.println(" INFO " + message);
    }

    void error(String message) {
        System.out.println(" ERROR " + message);
    }


}

class LoggerFactory {

    /**
     * 日志池
     */
    final static ConcurrentHashMap<String, JULLogger> namedLoggers = new ConcurrentHashMap<>();


    /**
     * 获取日志方法;
     */
    static JULLogger getLogger(String className) {

        if (namedLoggers.containsKey(className)) {
            return namedLoggers.get(className);
        } else {
            JULLogger julLogger = new JULLogger(className);
            namedLoggers.put(className, julLogger);
            return julLogger;
        }
    }


}


/**
 * date : 2024-09-12
 * 享元模式的经典用法,  日志框架
 *
 * @author : wushikai
 */
public class JulApplication {


    public static void main(String[] args) {

        JULLogger logger = LoggerFactory.getLogger(JulApplication.class.getName());
        System.out.println("logger.getName() = " + logger.getName());
        logger.debug("卧槽");
        logger.info("卧槽");
        logger.error("卧槽");

    }


}
